var APP_ITEM_HEIGHT = 28;
var MIN_PNAEL_HEIGHT = 11 * APP_ITEM_HEIGHT;
var SCROLL_HEIGHT = 4 * APP_ITEM_HEIGHT;
var SCREEN_MAX_APP_NUM = 32;
var moduleInfo = { "email": 1, "notify": 4, "news": 147, "vote": 119, "workflow": 5, "calendar": 8, "diary": 81, "attendance": 7, "bbs": 62 };
var moduleInfoIndex = { "a1": "email", "a4": "notify", "a147": "news", "a119": "vote", "a5": "workflow", "a8": "calendar", "a81": "diary", "a7": "attendance", "a62": "bbs" };
var availappicon = ["@crs", "address", "attendance", "attendance_manage", "bbs", "calendar", "comm", "default", "diary", "email", "erp", "exam_manage", "fax", "file_folder", "hrms", "info", "meeting", "mytable", "netdisk", "news", "notify", "notify_auditing", "person_info", "picture", "project", "reportshop", "roll_manage", "sale_manage", "score", "sms", "sys", "system", "todo", "training", "vehicle", "vote", "wf_destory", "wf_entrust", "wf_log", "wf_mine", "wf_new", "wf_query", "wf_stat", "wiki", "work_plan", "workflow", "ewp_baidu", "ewp_sina", "ewp_58", "ewp_weather", "ewp_qiyi", "ewp_gov", "ewp_tianya", "ewp_163", "ewp_taobao", "ewp_youku", "ewp_bdtieba", "ewp_sinas", "ewp_sinanews", "ewp_bdmp3", "ewp_xiami"];
var rowAppNum = 8;
var default_icon = "default";

function initModules(modules, el) {

  window.slideBox = jQuery("#container").slideBox({
    count: modules.length,
    cancel: isTouchDevice() ? "" : ".block",
    obstacle: "200",
    speed: "slow",
    touchDevice: isTouchDevice(),
    control: "#js_left_black_menu",
    listeners: {
      afterScroll: function(i) {
      },
      beforeScroll: function(i) {
        jQuery(".background").animate({
          left: -i * 70
        }, "normal");
      }
    }
  });
  el = jQuery(el);
  var count = 0;
  jQuery.each(modules || [], function(i, e) {
    var ul = jQuery("<ul></ul>");
    slideBox.getScreen(i).append(ul);
    jQuery.each(e.items || [], function(i, e) {
      addModule(e, ul);
    });
    i++;
  });
};

function isTouchDevice() {
  try {
    document.createEvent("TouchEvent");
    return true;
  } catch (e) {
    return false;
  }
};

function addModule(e, el) {
  el = jQuery(el);
  var _id = e.id;
  fixid = fixAppImage(_id);
  var li = jQuery("<li class=\"block\"></li>");
  var img = jQuery("<div class='img'><p><img src='../css/theme/11/images/app_icons/" + fixid + ".png' /></p></div>");
  var divT = jQuery("<div class=\"count\"></div>");
  li.attr("id", "block_" + e.func_id);
  li.attr("title", e.name);
  li.attr("index", e.func_id);
  var _margin = getAppMargin() + "px";
  li.css({ "margin-left": _margin, "margin-right": _margin });
  divT.attr("id", "count_" + e.func_id);
  if (e.count > 0) {
    divT.addClass("count" + e.count);
  }
  var a = jQuery("<a class=\"icon-text\" href=\"javascript: void(0)\"></a>");
  var span = jQuery("<span></span>").text(e.name);
  li.append(img.append(divT)).append(a.append(span));
  el.append(li);
};

function fixAppImage(e) {
  var els = availappicon;
  if (jQuery.inArray(e, els) == -1) {
    return default_icon;
  } else {
    return e;
  }
};

function refixAppPos() {
  var _margin = getAppMargin() + "px";
  jQuery("#container .screen li.block").css({ "margin-left": _margin, "margin-right": _margin });
};

function getAppMargin() {
  var clientSize = jQuery(document.body).outerWidth(true);
  var appsize = 120 * rowAppNum;
  if (clientSize > appsize) {
    var _margin = Math.floor((clientSize - appsize - 70 * 2) / 16);
  } else {
    var _margin = 0;
  }
  return _margin;
};

$(document).ready(function($) {

  $("body").focus();

  $("#overlay").height(window.document.documentElement.scrollHeight);
  initModules(modules);
  refixAppPos();
});